package com.mstore.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mstore.pojo.Consume;

public class LogConsumeExamineDao extends SuperDao {

	/**
	 * 修改审核通过标示
	 */
	public void updateLogConsumeExamineFlag(Consume consume) {
		StringBuilder sql = new StringBuilder();
		sql.append("update ubcenter.log_consume set examine_flag = ");
		sql.append(consume.getExamineFlag());
		sql.append(" where id = ");
		sql.append(consume.getId());

		executeUpdate(sql.toString());
	}

	/**
	 * 根据条件查询
	 */
	public List<Object> getLogConsumeExamineByCondition(Consume consume) {
		StringBuilder sql = new StringBuilder();
		sql.append("from Consume");
		sql.append(createSqlByCondition(consume));
		sql.append(" order by createTime desc");

		return find(sql.toString());
	}

	/**
	 * 根据条件查询消费总额
	 */
	public Double getSumMoneyByCondition(Consume consume) {
		StringBuilder sql = new StringBuilder();
		sql.append("select sum(money) from Consume");
		sql.append(createSqlByCondition(consume));

		List<Object> list = find(sql.toString());
		if (list.isEmpty()) {
			return 0D;
		}

		return (Double) list.get(0);
	}

	/**
	 * 查询同一小票号的个数
	 */
	public int getCountSmallBanks(Consume consume) {
		StringBuilder sql = new StringBuilder();
		sql.append("select count(id) from Consume");
		sql.append(createSqlByCondition(consume));

		List<Object> list = find(sql.toString());
		if (list.isEmpty()) {
			return 0;
		}

		Long count = (Long) list.get(0);
		if (count == null) {
			return 0;
		}

		return count.intValue();
	}

	/**
	 * 组装sql搜索语句
	 */
	private String createSqlByCondition(Consume consume) {
		String separate = " where ";
		StringBuilder sql = new StringBuilder();

		// 主键
		Long id = consume.getId();
		if (id != null) {
			sql.append(separate);
			sql.append("id = ");
			sql.append(id);
			separate = " and ";
		}

		// 企业id
		Long eaId = consume.getEaId();
		if (eaId != null) {
			sql.append(separate);
			sql.append("eaId = ");
			sql.append(eaId);
			separate = " and ";
		}

		// 店铺id
		Long shopId = consume.getShopId();
		if (shopId != null && shopId != 0) {
			sql.append(separate);
			sql.append("shopId = ");
			sql.append(shopId);
			separate = " and ";
		}

		// 操作员id
		Long userId = consume.getUserId();
		if (userId != null) {
			sql.append(separate);
			sql.append("userId = ");
			sql.append(userId);
			separate = " and ";
		}

		// 会员id
		Long memberId = consume.getMemberId();
		if (memberId != null) {
			sql.append(separate);
			sql.append("memberId = ");
			sql.append(memberId);
			separate = " and ";
		}

		// 会员手机号
		String memberMobile = consume.getMemberMobile();
		if (StringUtils.isNotBlank(memberMobile)) {
			sql.append(separate);
			sql.append("memberMobile = '");
			sql.append(memberMobile);
			sql.append("'");
			separate = " and ";
		}

		// 审核标示
		Integer examineFlag = consume.getExamineFlag();
		if (examineFlag != null) {
			sql.append(separate);
			sql.append("examineFlag = ");
			sql.append(examineFlag);
			separate = " and ";
		}

		// 小票号
		String smallBanks = consume.getSmallBanks();
		if (StringUtils.isNotBlank(smallBanks)) {
			sql.append(separate);
			sql.append("smallBanks = '");
			sql.append(smallBanks);
			sql.append("'");
			separate = " and ";
		}

		// 起始时间
		String beginTime = consume.getBeginTime();
		// 结束时间
		String endTime = consume.getEndTime();
		if (StringUtils.isNotBlank(beginTime) && StringUtils.isBlank(endTime)) {
			sql.append(separate);
			sql.append("createTime >= '");
			sql.append(beginTime);
			sql.append("'");
			separate = " and ";
		}
		else if (StringUtils.isBlank(beginTime)
				&& StringUtils.isNotBlank(endTime)) {
			sql.append(separate);
			sql.append("createTime <= '");
			sql.append(endTime);
			sql.append("'");
			separate = " and ";
		}
		else if (StringUtils.isNotBlank(beginTime)
				&& StringUtils.isNotBlank(endTime)) {
			sql.append(separate);
			sql.append("(createTime between '");
			sql.append(beginTime);
			sql.append("' and '");
			sql.append(endTime);
			sql.append("')");
			separate = " and ";
		}

		// 金额范围
		Double minMoney = consume.getMinMoney();
		Double maxMoney = consume.getMaxMoney();
		if (minMoney != null && maxMoney == null) {
			sql.append(separate);
			sql.append("money >= ");
			sql.append(minMoney);
			separate = " and ";
		}
		else if (minMoney == null && maxMoney != null) {
			sql.append(separate);
			sql.append("money <= ");
			sql.append(maxMoney);
			separate = " and ";
		}
		else if (minMoney != null && maxMoney != null) {
			sql.append(separate);
			sql.append("(money between ");
			sql.append(minMoney);
			sql.append(" and ");
			sql.append(maxMoney);
			sql.append(")");
			separate = " and ";
		}

		return sql.toString();
	}

}
